import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Like, Repository } from 'typeorm';
import GatherMachineAdjustment from '@main/entities/service/admin/gather/gather-machine-adjustment.entity';
import { ApiException } from '@main/common/exceptions/api.exception';
import { isEmpty } from 'lodash';
import { SysOperationLogService } from '../../system/operationLog/operationLog.service';

@Injectable()
export class GatherMachineAdjustmentService {
  constructor(
    @InjectRepository(GatherMachineAdjustment)
    private machineAdjustmentRepository: Repository<GatherMachineAdjustment>,
    private operationLogService: SysOperationLogService,
  ) {}

  async list() {
    return await this.machineAdjustmentRepository.find();
  }

  /**
   * 罗列所有配置
   */
  async page(data) {
    const { pageSize, pageNum, entity = {} } = data;

    const { machineAdjustment, machineCode } = entity;

    const where = {
      machineAdjustmentName: Like(`%${machineAdjustment}%`),
      machineAdjustmentCode: Like(`%${machineAdjustment}%`),
    };

    if (machineCode) {
      where['machineCode'] = machineCode;
    }

    return await this.machineAdjustmentRepository.findAndCount({
      where,
      order: {
        updateTime: 'DESC',
      },
      take: pageSize,
      skip: (pageNum - 1) * pageSize,
    });
  }

  /**
   * 新增
   */
  async add(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const machineAdjustment = await this.machineAdjustmentRepository.insert({
      ...data,
      createName: user.nickName,
      createUserName: user.userName,
      updateName: user.nickName,
      updateUserName: user.userName,
    });
    databaseOperationLogList.push({
      type: 'add',
      dataType: 'obj',
      database: 'GatherMachineAdjustment',
      checkOnlyKeyList: [],
      data: machineAdjustment,
      whereObj: null,
      remark: `GatherMachineAdjustment表新增数据`,
    });
    await this.operationLogService.add(
      {
        type: 'add',
        msg: `设备调机配置模块新增设备调机配置信息`,
        databaseOperationLogList,
        remark: `新增设备调机配置${data.machineAdjustmentName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 更新
   */
  async update(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];

    const { id, remark, machineAdjustmentName, machineAdjustmentJson } = data;

    await this.machineAdjustmentRepository.update(
      { id },
      {
        machineAdjustmentName,
        machineAdjustmentJson,
        remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      }
    );
    databaseOperationLogList.push({
      type: 'update',
      dataType: 'obj',
      database: 'GatherMachineAdjustment',
      checkOnlyKeyList: [],
      data: {
        machineAdjustmentName,
        machineAdjustmentJson,
        remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      },
      whereObj: { id },
      remark: `GatherMachineAdjustment表更新设备调机配置信息`,
    });
    await this.operationLogService.add(
      {
        type: 'update',
        msg: `设备调机配置模块新增设备调机配置信息`,
        databaseOperationLogList,
        remark: `更新设备调机配置${data.machineAdjustmentName}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 删除
   */
  async delete(idList, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];

    await this.machineAdjustmentRepository.delete(idList);

    databaseOperationLogList.push({
      type: 'delete',
      dataType: 'array',
      database: 'GatherMachineAdjustment',
      checkOnlyKeyList: [],
      data: null,
      whereObj: idList,
      remark: `GatherMachineAdjustment表删除设备调机配置信息`,
    });
    await this.operationLogService.add(
      {
        type: 'delete',
        msg: `设备调机配置模块删除设备调机配置信息`,
        databaseOperationLogList,
        remark: `删除设备调机配置`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  async isExistKey(data) {
    const { machineAdjustmentCode, machineCode } = data;

    const result = await this.machineAdjustmentRepository.findOne({
      where: { machineAdjustmentCode },
    });

    if (result) {
      throw new ApiException(10041);
    }

    const result1 = await this.machineAdjustmentRepository.findOne({
      where: { machineCode },
    });

    if (result1) {
      throw new ApiException(10050);
    }
  }

  /**
   * 详情
   */
  async info(data) {
    const machine: any = await this.machineAdjustmentRepository.findOne({
      where: { machineCode: data.machineCode },
    });

    if (isEmpty(machine)) {
      return null;
    }

    const { machineAdjustmentJson = null } = machine;

    return {
      ...machine,
      machineAdjustmentJson: machineAdjustmentJson ? JSON.parse(machineAdjustmentJson) : null,
    };
  }
}
